$(document).ready(function() {

    function updateTable(transactions_status) {
        var tbl   = $('#account_transaction_list table'),
            html  = ["<tbody class='list'>"],i,j = 1,l,item;

        if (transactions_status.length >= 1) {
            var row = 1;
            var total_in_amount = 0.0;
            var total_out_amount = 0.0;

            for (a = 0, b = transactions_status.length; a < b; ++a) {
                transaction_list = $.toJSON(transactions_status[a]);
                transactions = $.toJSON(transaction_list.records);

                if (transactions) {
                    for (i = 0, l = transactions.length; i < l; ++i) {
                        var transaction = transactions[i];
                        var trans_id = transaction.transaction_id;

                        if (row % 2 == 0)
                            html[j++] = '<tr class="even"><td>';
                        else
                            html[j++] = '<tr class="odd"><td>';

                        html[j++] = '<label id="trans_' + trans_id + '_account">' + transaction_list.account_name + '</label>';
                        html[j++] = '</td><td>';

                        if (transaction.favorite === "True")
                            html[j++] = '<span><img src="/img/star_on.gif" /></span>';
                        else
                            html[j++] = '<span><img src="/img/star_off.png" /></span>';
                        html[j++] = '<label id="trans_' + trans_id + '_desc">' + transaction.description + '</label>';
                        html[j++] = '</td><td>';
                        html[j++] = '<label id="trans_' + trans_id + '_amount">' + transaction.amount;
                        html[j++] = '</td><td>';
                        html[j++] = '<label id="trans_' + trans_id + '_is_income">' + transaction.is_income;
                        html[j++] = '</td><td>';
                        html[j++] = '<label id="trans_' + trans_id + '_tag">' + transaction.tag;
                        html[j++] = '</td><td>';
                        html[j++] = '<label id="trans_' + trans_id + '_date">' + transaction.transaction_date.substring(0, 10);
                        html[j++] = '</td></tr>';

                        row++;
                        if (transaction.is_income === "in")
                            total_in_amount += parseFloat(transaction.amount);
                        else
                            total_out_amount += parseFloat(transaction.amount);
                    }
                } else {
                    html[j++] = '<tr><td colspan="6">No Inventory data</td></tr>';
                }
                html[j] = "</tbody>";

            }
            $('#account_transaction_list table .list').replaceWith(html.join(''));
            // Update transaction list foot status
            var status_foot = '';

            status_foot += '<span>total count:' + row + '</span> | ';
            status_foot += '<span>total in amount:' + total_in_amount + '</span> | ';
            status_foot += '<span>total out amount:' + total_out_amount + '</span>';

            $('#account_transaction_list table tfoot').replaceWith('<tfoot><tr><th colspan="6">'+status_foot+'</th></tr></tfoot>');
        }
    };

    // Attach date query event TODO need move it in furture
    $('#transaction_form form #btn_commit').click(function (e) {
        e.preventDefault();

        $("#main").mask("Waiting...");

        var data = '';
        data += 'account_name=' + $('#transaction_form form #account_name').val();
        data += '&description=' + $('#transaction_form form #description').val();
        data += '&min_amount=' + $('#transaction_form form #min_amount').val();
        data += '&max_amount=' + $('#transaction_form form #max_amount').val();

        var income_val = "";
        $('#transaction_form form input.is_income').each(function(){
            if (this.checked == true) 
                income_val = $(this).val();
        });
        if (income_val === 'all') income_val = "";

        data += '&is_income=' + income_val;
        data += '&tag=' + $('#transaction_form form #tag').val();
        data += '&min_date=' + $('#transaction_form form #min_date').val();
        data += '&max_date=' + $('#transaction_form form #max_date').val();

        $.ajax({
            url: "/account/report/query",
            timeout: 10000,
            data: data,
            success: function(res) {
                    var transactions;
                    try {
                        var transactions_status = $.toJSON(res);

                        updateTable(transactions_status);
                    }
                    catch(e) {
                        alert("Error updating transaction data");
                    }

                    //$('#transaction_form form #btn_commit').set('disabled',false);
            },
            error : function () {
                alert("Error saving transaction data");
                //$('#transaction_form form #btn_commit').set('disabled',false);
            }
        });

        $("#main").unmask();
    });
});

